package com.itheima.admin.controller;


import com.itheima.admin.service.IDeptService;
import com.itheima.pojo.Config.Result;
import com.itheima.pojo.dto.DeptDto;
import com.itheima.pojo.dto.PageDto;
import com.itheima.pojo.vo.DeptVo;
import com.itheima.pojo.vo.PageVo;
import com.itheima.pojo.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itheima.pojo.dto.RoleDto;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门模块接口",tags = "admin_dept",description = "用于实现后台部门的增删改查操作")
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @PostMapping("/list")
    public PageVo<DeptVo> queryByPage(@RequestBody PageDto pageDto){
        if(pageDto.getCurrentPage()!=null){
            pageDto.setCurrentPage(1);
        }
        if(pageDto.getPageSize()!=null){
            pageDto.setPageSize(10);
        }
        return deptService.queryByPage(pageDto);
    }

    @GetMapping("/listall")
    public List<DeptVo> selectAll(){
        return deptService.selectAll();
    }

    @PostMapping("/add")
    public Result addDept(@RequestBody DeptDto deptDto,HttpSession session){
        boolean flag=deptService.addDept(deptDto,session);
        Result result=Result.builder()
                .flag(flag)
                .data(new DeptVo(session.getAttribute("deptId").toString(),
                                deptDto.getDeptName(),
                                deptDto.getParentId(),
                                deptDto.getStatus()))
                .message(flag?"添加部门成功!":"添加部门失败!")
                .build();
        return result;
    }

    @PutMapping("/edit")
    public Result updateDept(@RequestBody DeptDto deptDto,HttpSession session){
        boolean flag=deptService.updateDept(deptDto,session);
        Result result=Result.builder()
                .flag(flag)
                .data(deptDto)
                .message(flag?"修改部门成功!":"修改部门失败")
                .build();
        return result;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除部门")
    public Result deleteById(@PathVariable("id") String id){
        boolean flag=deptService.deleteById(id);
        Result result = Result.builder()
                .flag(flag)
                .message(flag?"删除用户成功!":"删除用户失败")
                .build();
        return result;
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询部门")
    public DeptVo queryById(@PathVariable("id") String id){
        return deptService.queryById(id);
    }
}
